﻿import game.DataManager;
import game.IntroScreen;
import game.Timer;
import game.HintDoor;
import game.Letter;
import game.Main;
import game.Match;
import game.SandClock;
import game.Session;
import game.TimerClass;
import game.Word;
import mx.controls.Loader;
import mx.utils.Delegate;
/**
* ...
* @author Default
*/

class game.GUI {
	
	public var _mcInGame:MovieClip;
	public var _mcDialogs:MovieClip;
	public var _mcLayerTiles:MovieClip;
	public var _mcLayerInterface:MovieClip;
	private var _mcContainer:MovieClip;
	public var _cTimer:Timer;
	public var _canClick:Boolean = false;
	public var _canClear:Boolean = false;
	private var _playing:Boolean = true;
	private var _cDoor:HintDoor;
	private var _cIntro:IntroScreen
	private var _mcEmptyTiles:MovieClip;
	private var _aEmptyTiles:Array;
	private var _nCurrentHint:Number = 1;
	private var _nNextHint:Number = 2;
	
	public function set Theme(srt:String):Void {
		_mcInGame._smcTheme._smcThemeText._smcText._stxText.text = srt;
	}
	
	private static var _cInstance:GUI;
	public static function getInstance():GUI {
		return _cInstance;
	}
	
	public function GUI(mcContainer:MovieClip) {
		_mcContainer = mcContainer;
		_cInstance = this;
		
		_mcInGame	 			= _mcContainer.attachMovie("InGame", "inGame", _mcContainer.getNextHighestDepth() );

		_mcLayerTiles	 		= _mcContainer.createEmptyMovieClip("tilelayer", _mcContainer.getNextHighestDepth() );
		
		_mcLayerInterface		= _mcContainer.attachMovie("Interface", "interface", _mcContainer.getNextHighestDepth() );
		_mcLayerInterface._x	= 8.8;
		_mcLayerInterface._y	= 24.9; 
		
		_mcDialogs			= _mcContainer.attachMovie("dialogs", "dialogs", _mcContainer.getNextHighestDepth() );
		_mcDialogs._x 		= -157.8;
		_mcDialogs._y 		= 1.5;
		
		_cDoor 				= new HintDoor(_mcInGame.hintdoor, parseInt( Main.getInstance()._cAppConfigFile.getData ( "hintRechargeLapse") ));
		
		_cTimer 			= new Timer(_mcLayerInterface._smcTimeBar, parseInt( Main.getInstance()._cAppConfigFile.getData ( "roundTime") ), Delegate.create (this, timesUp)); // HERNAN
		
		_mcLayerInterface._stxScore._stxScore.text = "0";
		
		_mcInGame.roundcomplete = _mcDialogs.roundcomplete;
		_mcInGame.optionsdialog = _mcDialogs.optionsdialog;
		
		_mcInGame._smcTheme.gotoAndStop("hidden");
		
		createEmptyTiles();
		
		_cIntro = new IntroScreen( _mcContainer );
		
		setListeners();
	}
	
	public function updateScore():Void {
		_mcLayerInterface._stxScore._stxScore.text = DataManager.getInstance().getScore();
	}
	
	public function resetDoor():Void {
		_cDoor.resetPosition();
	}
	
	public function setRound(round:Number):Void {
		_mcLayerInterface._sbtRound.roundtext.text = round;
	}
	
	public function timesUp():Void {
		trace ("You lose :-(");
		_canClick = false;
		//_playing = false;
	}
	
	public function showingTheme():Void {
		_mcInGame._smcTheme.gotoAndPlay("showing");
	}
	
	public function showTheme():Void {
		_mcInGame._smcTheme.gotoAndPlay("show");
		Theme = "";
	}
	
	public function hidingTheme():Void {
		_mcInGame._smcTheme.gotoAndPlay("hiding");
	}
	
	public function hiddenTheme():Void {
		_mcInGame._smcTheme.gotoAndStop("hidden");
	}
	
	public function get layerTiles():MovieClip { return _mcLayerTiles; };
		
	public function onClickEnter():Void {
		Main.getInstance()._cSoundManager.clickStart();
		if (_canClick && _playing && Session.getInstance()._cCurrentMatch._firstWord.length > 0) {
			
			Session.getInstance()._cCurrentMatch._originalWord.doJumpLeft();
			//SandClock.create( 30 * 1.5 , Delegate.create(this, checkIfRoundCompleted) );
			checkIfRoundCompleted();
			_canClick = false;
		}
	}
	
	private function onClickOptions():Void {
		Main.getInstance()._cSoundManager.clickStart();
		_mcDialogs.optionsdialog.gotoAndPlay("options");
	}
	
	private function onClickHelp():Void {
		Main.getInstance()._cSoundManager.clickStart();
		
		_mcDialogs.helpdialog.gotoAndPlay("help1");


	}
	
	private function onClickQuit():Void {
		Main.getInstance()._cSoundManager.clickStart();
		
		_mcDialogs.quitdialog.gotoAndPlay("quit");
	}

	
	private function checkIfRoundCompleted():Void {
		
		if ( Session.getInstance()._cCurrentMatch.haveWon() ) {
			showRoundComplete();
			_canClick = false;
			_canClear = false;
			Main.getInstance()._cSoundManager.correctStart();
		} else {
			Main.getInstance()._cSoundManager.incorrectStart();
		}
	}
	
	public function showHelp(object:Object, onFinish:Function) {
		
		_mcDialogs.helpdialog.gotoAndPlay("help1");
		_mcDialogs.helpdialog.finish = Delegate.create(object, onFinish);
	}
		
	private function showRoundComplete():Void {
	
		_mcInGame.roundcomplete.roundComplete = Delegate.create( this , onRoundCompleteOk );
		_mcInGame.roundcomplete.gotoAndStop("round");
		_mcInGame.roundcomplete.theme = Session.getInstance()._cCurrentMatch._currentDefinition._sTheme;
		_mcInGame.roundcomplete.timeBonus = (_cTimer.stop())?  "+15" : "0";
		_mcInGame.roundcomplete.roundScore = String(_cTimer.getRoundScore());
		_mcInGame.roundcomplete.word1 = Session.getInstance()._cCurrentMatch._currentDefinition._sFirstWord;
		_mcInGame.roundcomplete.word2 = Session.getInstance()._cCurrentMatch._currentDefinition._sSecondWord;
		_mcInGame.roundcomplete.title = "ROUND " + Session.getInstance()._nRound + " COMPLETE";
		_cDoor.stop();
	}
	
	private function createEmptyTiles():Void {
		_aEmptyTiles = new Array;
			_mcEmptyTiles = _mcLayerTiles.createEmptyMovieClip( "emptyTiles", _mcLayerInterface.getNextHighestDepth() );
			for (var i:Number = 0; i < 15; i++) {
				var cLetter:Letter =  new Letter( "", _mcEmptyTiles, i);
				_aEmptyTiles.push ( cLetter );
				cLetter.appear();
			}
	}
	
	public function hideEmptyTiles(max:Number):Void {
		trace("MAX: " + max);
		for (var i:Number = 0; i < max+1; i++) {
			Letter( _aEmptyTiles[i] ).hide();
		}
		_canClick = true;
		_canClear = true;
	}
	
	public function showEmptyTiles():Void {
		for (var i:Number = 0; i < 15; i++) {
			Letter( _aEmptyTiles[i] ).show();
		}
	}
	
	private function onRoundCompleteOk():Void {
		Session.getInstance()._cCurrentMatch.roundCompleteOk();
	
		SandClock.create( 30 , Delegate.create(this, hidingComplete) );
		
	}
	
	private function hidingComplete():Void {
		_canClick = false;
		_cTimer.reset();
		_cDoor.start();
		_cTimer.start(  Number( Main.getInstance()._cAppConfigFile.getData ( "roundTime") ) );
		Session.getInstance().nextMatch();
	}
	
	public function onClickBackspace():Void {
		Main.getInstance()._cSoundManager.clickStart();
		Session.getInstance()._cCurrentMatch.goBack();
		_canClick = true;
		
	}
	
	public function loadBothImages(_url1:String, _url2:String):Void {
		_mcInGame._smcLoadedBitmap1.loadMovie(_url1);
		_mcInGame._smcLoadedBitmap2.loadMovie(_url2);
	}
	
	public function loadNextImage(_url:String):Void {
		_mcInGame["_smcLoadedBitmap" + _nNextHint].loadMovie(_url);
	}
	
	public function switchHints():Void {
		var tempHint = _nCurrentHint;
		_nCurrentHint = _nNextHint;
		_nNextHint = tempHint;
		
		_mcInGame._smcLoadedBitmap1.visible = _nCurrentHint == 1;
		_mcInGame._smcLoadedBitmap2.visible = _nCurrentHint == 2;
	}
	
	public function onClickClear():Void {
		Main.getInstance()._cSoundManager.clickStart();
		
		if (_canClear && Session.getInstance()._cCurrentMatch._firstWord.length > 0) {
			Session.getInstance()._cCurrentMatch.clear();
			_canClick = true;
			trace ("cleared")
		}
	}
	
	private function setListeners():Void {
		Button(_mcInGame._sbtEnter).onRelease = Delegate.create(this, onClickEnter);
		Button(_mcInGame._sbtEnter).onRollOver = Delegate.create(this, onOver);
		
		Button(_mcInGame._sbtBackspace).onRelease = Delegate.create(this, onClickBackspace);
		Button(_mcInGame._sbtBackspace).onRollOver = Delegate.create(this, onOver);
		
		Button(_mcInGame._sbtClear).onRelease = Delegate.create(this, onClickClear);
		Button(_mcInGame._sbtClear).onRollOver = Delegate.create(this, onOver);
		
		Button(_mcInGame._sbtOptions).onRelease = Delegate.create(this, onClickOptions);
		Button(_mcInGame._sbtOptions).onRollOver = Delegate.create(this, onOver);
		
		Button(_mcInGame._sbtHelp).onRelease = Delegate.create(this, onClickHelp);
		Button(_mcInGame._sbtHelp).onRollOver = Delegate.create(this, onOver);
		
		Button(_mcInGame._sbtQuit).onRelease = Delegate.create(this, onClickQuit);
		Button(_mcInGame._sbtQuit).onRollOver = Delegate.create(this, onOver);
		
	}
	
	private function onOver():Void {
		Main.getInstance()._cSoundManager.rolloverStart();
	}
	
}